package com.hecai.scanning.controller;

import com.alibaba.fastjson.JSONObject;
import com.hecai.scanning.config.ConfigProperties;
import com.hecai.scanning.util.HttpCode;
import com.hecai.scanning.util.HttpUtil;
import com.hecai.scanning.util.Rest;
import com.hecai.scanning.vo.NotifyMessage;
import com.hecai.scanning.websocket.MessageComponent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * 控制器
 *
 * @author VitalYang
 * @create 2018-12-07 11:07
 */
@Slf4j
@Controller
public class ScannerController
{
    @Autowired
    private ConfigProperties configProp;

    @Autowired
    private MessageComponent messageComponent;

    @RequestMapping("/index")
    public String index()
    {
        return "index";
    }

    /**
     * 接收服务器发送的数据接口
     *
     * @param params json格式的map格式
     * @return
     */
    @ResponseBody
    @RequestMapping("/recvServerMsgDoor")
    public String recvServerMsgDoor(String params)
    {
        if (null == params || "".equals(params))
        {
            return "false";
        }
        try
        {
            Map paramMap = JSONObject.parseObject(params, Map.class);
            Integer notifyType = (Integer) paramMap.get("notifyType");
            NotifyMessage message = new NotifyMessage();
            message.setNotifyType(notifyType);
            message.setNotifyData(paramMap);
            messageComponent.sendMsg(message);
        } catch (Exception e)
        {
            return "false";
        }
        return "true";
    }

    @ResponseBody
    @RequestMapping("/savePicture")
    public Rest savePicture(String imgData, String savePath, String imgName) throws Exception
    {
        String centerIpaddr = configProp.getCenterIpAddr();
        String centerSp = configProp.getCenterSavePic();
        String withHall = configProp.getHallNumber();
        String windowNo = configProp.getWindowNumber();
        if (null == centerSp)
        {
            return Rest.newBuilder().code(HttpCode.ILLEGAL_PARAMETERS).build();
        }
        Map<String, Object> params = new HashMap<>();
        params.put("imgData", imgData);
        params.put("savePath", savePath);
        params.put("imgName", imgName);
        params.put("withHall", withHall);
        params.put("windowNo", windowNo);
        String res = HttpUtil.doPost(centerIpaddr + centerSp, params);
        Map<String, Object> resData = JSONObject.parseObject(res, Map.class);
        if (null == resData)
        {
            return Rest.newBuilder().code(HttpCode.ILLEGAL_PARAMETERS).build();
        }
        Integer code = (Integer) resData.get("code");
        if (code == 200)
        {
            return Rest.newBuilder().code(HttpCode.OK).build();
        } else if (code == 8007)
        {
            log.error("图片保存成功，但未查到任何订单信息，图片信息入库失败");
            return Rest.newBuilder().code(HttpCode.PICNAME_NOTMATCH_ORDERID).build();
        } else
        {
            return Rest.newBuilder().code(HttpCode.ILLEGAL_PARAMETERS).build();
        }
    }
}
